﻿@using System;
@using System.Data
@using System.Data.Entity;
@using Kendo.Mvc.UI;
@using WarOfDev.Web.ViewModels;
@model IEnumerable<DeveloperViewModel>
@{
    ViewBag.Title = "Developers list";
}
<h2>
    All Developers</h2>
@(Html.Kendo().Grid(Model)
    .Name("Grid")
    .Columns(columns =>
    {
        columns.Bound(d => d.Id).Width(50).Hidden();
        columns.Bound(d => d.GameLevel).Width(55).ClientTemplate("<div class=\"level-head level-#=GameLevel#\">&nbsp;<div>");
        columns.Bound(d => d.NickName);
        columns.Bound(d => d.DomainAccount);
        columns.Bound(d => d.TrueName);
        columns.Bound(d => d.ExperienceValue).Width(100); ;
        columns.Bound(d => d.IsLocked).Width(90);
        columns.Command(command => { command.Edit(); command.Destroy(); }).Width(190).Title("Operation");

    })
   .ClientDetailTemplateId("developersTemplate")
   .ToolBar(toolbar => toolbar.Create().Text("Create new user"))
    .Editable(editable => editable.Mode(GridEditMode.InLine))
    .Pageable(p => p.Numeric(true).PageSizes(true))
    .Sortable(s => s.Enabled(true).SortMode(GridSortMode.MultipleColumn))
    .DataSource(dataSource => dataSource
        .Ajax()
        .Events(e => e.Error("errorhandler"))
        .Model(model =>
        {
            model.Id(d => d.Id);
            model.Field(d => d.Id).Editable(false);
            model.Field(d => d.GameLevel).Editable(false);
            model.Field(d => d.ExperienceValue).DefaultValue(50);
        })
        .Create(update => update.Action("Create", "Developer"))
        .Read(read => read.Action("DevelopersRead", "Developer"))
        .Update(update => update.Action("Edit", "Developer"))
        .Destroy(update => update.Action("Delete", "Developer"))
        .ServerOperation(false)
    )

)
<script>
    function dataBound() {
        this.expandRow(this.tbody.find("tr.k-master-row").first());
    }
</script>
<script id="developersTemplate" type="text/kendo-tmpl">
    @(Html.Kendo().TabStrip()
            .Name("TabStrip_#=Id#")
            .SelectedIndex(0)
            .Items(items =>
            {
                items.Add().Text("Developer #= TrueName #").Content(
                    "<div class='developer-details'>" +
                        "<ul>" +
                            "<li><div class=\"level-72-head level-72-#=GameLevel#\">&nbsp;<div></li>" +
                            "<li>#= DomainAccount #</li>" +
                            "<li><label>Experience Value:</label>#= ExperienceValue #</li>" +
                            "<li><label>True Name:</label>#= TrueName #</li>" +
                            "<li><label>Nick Name:</label>#= NickName #</li>" +
                            "<li><label>Created On:</label>#= CreatedOnToString #</li>" +
                             "<li><label>Created By:</label>#= CreatedBy #</li>" +
                            "<li><label>Modified On:</label>#= ModifiedOnToString #</li>" +
                            "<li><label>Modified By:</label>#= ModifiedBy #</li>" +
                        "</ul>" +
                    "</div>"
                );

                items.Add().Text("Tasks").Content(@<text>
                    @(Html.Kendo().Grid<TaskViewModel>()
                        .Name("Tasks_#=Id#")
                        .Columns(columns =>
                        {
                            columns.Bound(t => t.Id).Width(50);
                            columns.Bound(t => t.Title);
                            columns.Bound(t => t.Status).Width(80);

                        })
                        .DataSource(dataSource => dataSource
                            .Ajax()
                            .Read(read => read.Action("GetTasksByDevId", "Developer", new { devId = "#=Id#" }))
                        )
                        .Selectable(selectable => selectable.Mode(GridSelectionMode.Multiple))
                        .Pageable()
                        .Sortable()
                        .ToClientTemplate())
                </text>
              );

            })
            .ToClientTemplate())
</script>
<script>
    function dataBound() {
        this.expandRow(this.tbody.find("tr.k-master-row").first());
    }
</script>
<script type="text/javascript">
    function errorhandler(e) {
        if (e.errors) {
            var message = "Errors:\n";
            $.each(e.errors, function (key, value) {
                if ('errors' in value) {
                    $.each(value.errors, function () {
                        message += this + "\n";
                    });
                }
            });
            alert(message);
        }
    }
</script>
